# -*- coding: utf-8 -*-


import os
import datetime
from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.shared import Pt, RGBColor
from docx.oxml.ns import qn
import docx


"""
写word
参考：https://blog.csdn.net/lys_828/article/details/104116255
"""


doc = Document()
heading_1 = '请 假 条'
paragraph_1 = doc.add_heading(heading_1, level = 1)
paragraph_1.alignment = WD_ALIGN_PARAGRAPH.CENTER


greeting_word = '尊敬的领导：'
paragraph_2 = doc.add_paragraph(greeting_word)


name = input("请输入请假人姓名：")
division = input("请输入所属部门：")
reason = input("请输入请假事由：")
day = input('请输入请假天数：')
detailed_time = input("请输入请假的具体时间：")

word_1 = "    本人"
word_2 = "，所在部门"
word_3 = "，由于"
word_4 = "，需请假"
word_5 = "天，请假的具体时间为"
word_6 = "。"

paragraph_3 = doc.add_paragraph()
paragraph_3.add_run(word_1)
paragraph_3.add_run("{0: ^{1}}".format(name,len(name) + 6)).underline = True
paragraph_3.add_run(word_2)
paragraph_3.add_run("{0: ^{1}}".format(division,len(division) + 6)).underline = True
paragraph_3.add_run(word_3)
paragraph_3.add_run("{0: ^{1}}".format(reason,len(reason) + 6)).underline = True
paragraph_3.add_run(word_4)
paragraph_3.add_run("{0: ^{1}}".format(day,len(day) + 6)).underline = True
paragraph_3.add_run(word_5)
paragraph_3.add_run("{0: ^{1}}".format(detailed_time,len(detailed_time) + 6)).underline = True
paragraph_3.add_run(word_6)
paragraph_3.paragraph_format.line_spacing = 1.5



word_7 = '申请人：'
paragraph_4 = doc.add_paragraph()
paragraph_4.add_run(word_7)
paragraph_4.add_run("{0: ^{1}}".format(name,len(name) + 6)).underline = True
paragraph_4.alignment = WD_ALIGN_PARAGRAPH.RIGHT


word_8 = '日期：'
today_year = datetime.datetime.today().year
today_month = datetime.datetime.today().month
today_day = datetime.datetime.today().day
sign_date = "{}年{}月{}日".format(today_year,today_month,today_day)

paragraph_5 = doc.add_paragraph()
paragraph_5.add_run(word_8)
paragraph_5.add_run("{0: ^{1}}".format(sign_date,len(sign_date) + 6)).underline = True
paragraph_5.alignment = WD_ALIGN_PARAGRAPH.RIGHT

for paragraph in doc.paragraphs:
    for run in paragraph.runs:
        run.font.color.rgb = RGBColor(0,0,0)
        run.font.name = '微软雅黑'
        r = run._element.rPr.rFonts
        r.set(qn('w:eastAsia'),'微软雅黑')

doc.save("{}-test_model.docx".format(name))

print("-"*30)
print("\n{}的请假条已经生成\n\n文件位置在：{}\n\n文件名称为：{}-请假条.docx".format(name,os.getcwd(),name))